﻿using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Tools
{
    /// <summary>
    /// 文本操作类，该类无法被继承且无法实例化
    /// </summary>
    sealed public class EasyText
    {
        private EasyText() { }

        /// <summary>
        /// 取文本左边
        /// <para>从源文本(sourceText)中将目标文本(targetText)左边的文本取出来并返回该文本。如果目标文本找不到则返回空字符串。</para>
        /// <para>示例：Left("123456789abcdefg","56") 返回 1234</para>
        /// </summary>
        /// <param name="sourceText">源文本</param>
        /// <param name="targetText">欲寻找文本</param>
        /// <param name="startIndex">起始寻找位置，位置从0开始。如果该参数留空则从0开始。</param>
        /// <param name="ignoreCase">忽略大小写。true = 忽略大小写。</param>
        /// <example></example>
        /// <example>示例：GetLeft("123456789abcdefg","56") 返回 1234</example>
        public static string Left(string sourceText, string targetText, int startIndex = 0, bool ignoreCase = false)
        {
            //异常检查
            if (
                sourceText.Length == 0 ||
                targetText.Length == 0 ||
                sourceText.Length < targetText.Length ||
                startIndex < 0 ||
                startIndex > (sourceText.Length - targetText.Length)
                )
                return "";

            //处理大小写，如果忽略大小写则转成小写
            string backup_sourceText = sourceText;
            if (ignoreCase)
            {
                sourceText = sourceText.ToLower();
                targetText = targetText.ToLower();
            }

            //寻找指定文本
            int p = sourceText.IndexOf(targetText, startIndex);
            if (p == -1)
                return "";
            return backup_sourceText.Substring(0, p);
        }

        /// <summary>
        /// 取文本右边
        /// <para>从源文本(sourceText)中将目标文本(targetText)右边的文本取出来并返回该文本。如果目标文本找不到则返回空字符串。</para>
        /// <para>示例：Right("123456789abcdefg","789") 返回 abcdefg</para>
        /// </summary>
        /// <param name="sourceText">源文本</param>
        /// <param name="targetText">欲寻找文本</param>
        /// <param name="startIndex">起始寻找位置，位置从0开始。如果该参数留空则从0开始。</param>
        /// <param name="ignoreCase">忽略大小写。true = 忽略大小写。</param>
        public static string Right(string sourceText, string targetText, int startIndex = 0, bool ignoreCase = false)
        {
            if (
                sourceText.Length == 0 ||
                targetText.Length == 0 ||
                sourceText.Length < targetText.Length ||
                startIndex < 0 ||
                startIndex > (sourceText.Length - targetText.Length)
                )
                return "";

            string backup_sourceText = sourceText;
            if (ignoreCase)
            {
                sourceText = sourceText.ToLower();
                targetText = targetText.ToLower();
            }

            int p = sourceText.IndexOf(targetText, startIndex);
            if (p == -1)
                return "";
            return backup_sourceText.Substring(p + targetText.Length, sourceText.Length - p - targetText.Length);
        }

        /// <summary>
        /// 取文本中间
        /// <para>从源文本(sourceText)中将左边文本(leftText)和右边文本(rightText)之间的文本取出来并返回该文本。如左边文本和右边文本有一个找不到则返回空字符串。</para>
        /// <para>示例：Middle("123456789abcdefg","4","c")  返回 56789ab</para>
        /// </summary>
        /// <param name="sourceText">源文本</param>
        /// <param name="leftText">左边文本</param>
        /// <param name="rightText">右边文本</param>
        /// <param name="beginIndex">起始寻找位置，位置从0开始。如果该参数留空则从0开始。</param>
        /// <param name="ignoreCase">忽略大小写。true = 忽略大小写。</param>
        public static string Middle(string sourceText, string leftText, string rightText, int beginIndex=0, bool ignoreCase=false)
        {
            if (
                sourceText.Length == 0 ||
                leftText.Length == 0 ||
                rightText.Length == 0 ||
                sourceText.Length < leftText.Length ||
                sourceText.Length < rightText.Length ||
                beginIndex < 0 ||
                beginIndex > (sourceText.Length - leftText.Length - rightText.Length)
                )
                return "";

            string backup_sourceText = sourceText;
            if (ignoreCase)
            {
                sourceText = sourceText.ToLower();
                leftText = leftText.ToLower();
                rightText = rightText.ToLower();
            }

            int leftPos = sourceText.IndexOf(leftText, beginIndex);
            int rightPos = sourceText.IndexOf(rightText, leftPos + leftText.Length);

            if (leftPos == -1 || rightPos == -1)
                return "";
            return backup_sourceText.Substring(leftPos + leftText.Length, rightPos - leftPos - leftText.Length);
        }

    }
}
